package com.haier.nengyuan.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;



import java.util.Calendar;

public class EarningDetailActivity01 extends BaseActivity implements View.OnClickListener {
    private static final long cycle = 2 * 60 * 60 * 1000;
    private ImageView mTitleLeft;
    private TextView mTitleCenter;
    private TextView mTitleRight;
    private TextView mTvTimeDetail;
    private RadioButton mRbDay;
    private RadioButton mRbWeek;
    private RadioButton mRbMonth;
    private RadioButton mRbYear;
    private long time;

    private BaseFragment[] mAllFrags;
    private Handler mHandler = new Handler() { //导os包
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    mHandler.sendEmptyMessageDelayed(1, cycle);
                    refreshView();
                    break;
                default:
                    break;
            }
        }
    };

    private void refreshView() {
        for (BaseFragment fragment : mAllFrags) {
            if (fragment != null && fragment.isVisible()) {
                fragment.onResume();
            }
        }
    }

    private void refreshData() {
        long xm = cycle - System.currentTimeMillis() % cycle;
        Message mMessag = new Message();
        mMessag.what = 1;
        mHandler.sendMessageDelayed(mMessag, xm);//两小时
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_energy_total);
        initViews();
        initFragment();
        refreshData();
    }

    private void initViews() {
        mTitleLeft = (ImageView) findViewById(R.id.title_left);
        mTitleCenter = (TextView) findViewById(R.id.title_center);
        mTitleRight = (TextView) findViewById(R.id.title_right);
        mTitleLeft.setOnClickListener(this);
        mTitleCenter.setText("累计收益");
        mTitleRight.setVisibility(View.INVISIBLE);

        mTvTimeDetail = (TextView) findViewById(R.id.tv_time_detail);
        mRbDay = (RadioButton) findViewById(R.id.rb_day);
        mRbWeek = (RadioButton) findViewById(R.id.rb_week);
        mRbMonth = (RadioButton) findViewById(R.id.rb_month);
        mRbYear = (RadioButton) findViewById(R.id.rb_year);

        mRbDay.setOnClickListener(this);
        mRbWeek.setOnClickListener(this);
        mRbMonth.setOnClickListener(this);
        mRbYear.setOnClickListener(this);

    }

    private void initFragment() {
        mAllFrags = new BaseFragment[]{
                new BarChartFragment01(),
                new WeekBarChartFragment01(),
                new MonthFragment(),
                new YearFragment()};
        addFragment(R.id.chart_fragments, mAllFrags[0]);
        mTvTimeDetail.setText("0时-24时");
        mRbDay.setChecked(true);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        time = System.currentTimeMillis();
        if (id == R.id.rb_day) {
            showFrag(mAllFrags[0]);
            mTvTimeDetail.setText(R.string.energry_total_acitvity_timeperiod);
        } else if (id == R.id.rb_week) {
            showFrag(mAllFrags[1]);
            Calendar rightNow = Calendar.getInstance();
            int week = rightNow.get(rightNow.DAY_OF_WEEK) - 1;
            if (week == 0) {
                week = 7;
            }
            long bef = (week - 1) * 24 * 60 * 60 * 1000;
            long later = (7 - week) * 24 * 60 * 60 * 1000;

            mTvTimeDetail.setText(MyDateUtil.changeM(time - bef) +
                    getResources().getString(R.string.energry_total_acitvity_unit_month) +
                    MyDateUtil.changeD(time - bef) +
                    getResources().getString(R.string.energry_total_acitvity_unit_day) + " - " +
                    MyDateUtil.changeM(time + later) +
                    getResources().getString(R.string.energry_total_acitvity_unit_month) +
                    MyDateUtil.changeD(time + later) +
                    getResources().getString(R.string.energry_total_acitvity_unit_day));
        } else if (id == R.id.rb_month) {
            showFrag(mAllFrags[2]);
            mTvTimeDetail.setText(getResources().getString(R.string.energry_total_acitvity_timeperiod2) +
                    " - " + MyDateUtil.changeM(time) +
                    getResources().getString(R.string.energry_total_acitvity_unit_month) +
                    MyDateUtil.changeD(time) +
                    getResources().getString(R.string.energry_total_acitvity_unit_day));
        } else if (id == R.id.rb_year) {
            showFrag(mAllFrags[3]);

            long time = System.currentTimeMillis();
            int year = Integer.valueOf(MyDateUtil.changeY(time));
            if (year == 2017) {
                mTvTimeDetail.setText(R.string.energry_total_acitvity_year);
            } else if (year == 2018) {
                mTvTimeDetail.setText(R.string.energry_total_acitvity_2018year);
            }

        } else if (id == R.id.title_left) {
            finish();
        }
    }

    public void showFrag(BaseFragment desFragment) {
        for (BaseFragment baseFragment : mAllFrags) {
            if (baseFragment == desFragment) {
                if (!desFragment.isAdded()) {
                    addFragment(R.id.chart_fragments, desFragment);
                }
                showFragment(desFragment);
            } else {
                hideFragment(baseFragment);
            }
        }
    }
}
